﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using EcSalseManage.App_Code;

namespace EcSalseManage.EcSalse
{
    public partial class Report_UpLoad : System.Web.UI.Page
    {
        static string id = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string ExchangeTypeID = Request.QueryString["typeid"].ToString();
                id = Request.QueryString["id"].ToString();
                this.lbtnUpLoad.Attributes.Add("OnClick", "return DataCheck();");
                this.Report_Tab1.ExchangeTypeID = ExchangeTypeID;
                this.Report_Tab1.MainID = id;
                BindGrid();
            }
        }

        #region BindGrid
        private void BindGrid()
        {
            DataSet ds = new DataSet();
            DataAccess.Operate.DA_Report_Attach_Inherit Report_Attach_Inherit = new DataAccess.Operate.DA_Report_Attach_Inherit();

            dgList.DataBind();

            ds = Report_Attach_Inherit.GetAllByReportID(id);
            dgList.DataSource = ds;
            dgList.DataBind();
        }
        #endregion

        protected void lbtnUpLoad_Click(object sender, EventArgs e)
        {
            DataAccess.Operate.DA_Report_Attach_Operate Report_Attach_Operate = new DataAccess.Operate.DA_Report_Attach_Operate();
            DataEntity.T_Report_Attach Report_Attach = new DataEntity.T_Report_Attach();
            string sFileName = "";
            string sFilePath = "";
            string sFileType = "";
            Guid gProjectID = new Guid(id);
            if (!Directory.Exists("D:\\ReportManager\\" + DateTime.Now.Year.ToString()))
            {
                Directory.CreateDirectory("D:\\ReportManager\\" + DateTime.Now.Year.ToString());
            }
            if (!Directory.Exists("D:\\ReportManager\\" + DateTime.Now.Year.ToString() + "\\" + id))
            {
                Directory.CreateDirectory("D:\\ReportManager\\" + DateTime.Now.Year.ToString() + "\\" + id);
            }

            sFileType = CommonFunction.CheckFileType(this.txtFilePath.PostedFile.FileName);
            if (sFileType != "")
            {
                sFileName = id + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + sFileType;
            }
            if (sFileName == "")
            {
                Response.Write("<script>alert('上传的文件格式错误,请重新选择！');</script>");
                return;
            }
            if (!CommonFunction.CheckFileSize(long.Parse(this.txtFilePath.PostedFile.ContentLength.ToString()), 20))
            {
                Response.Write("<script>alert('上传的文件大小超过20M,请重新上传！');</script>");
                return;
            }
            sFilePath = "\\ReportManager\\" + DateTime.Now.Year + "\\" + id + "\\" + sFileName;
            try
            {
                Report_Attach.Project_Report_FileDesc = this.txtProjectDesc.Text.Trim();
                Report_Attach.Project_Report_FileName = sFileName;
                Report_Attach.Project_Report_FilePath = sFilePath;
                Report_Attach.Project_Report_ExchangeTypeID =Convert.ToInt32(this.Report_Tab1.ExchangeTypeID);
                Report_Attach.Project_Report_FileID = 1;
                Report_Attach.Project_Report_MainID = new Guid(id);
                Report_Attach_Operate.Insert(Report_Attach);
                this.txtFilePath.PostedFile.SaveAs("D:\\ReportManager\\" + DateTime.Now.Year.ToString() + "\\" + id + "\\" + sFileName);
                DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 5, new Guid(id), 4);
                BindGrid();
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { 
                
            }
        }

        protected void dgList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (e.Item.ItemIndex > -1)
                {
                    ImageButton imgbtnDelete = (ImageButton)e.Item.FindControl("imgbtnDelete");
                    imgbtnDelete.Attributes.Add("onclick", "return window.confirm(\"是否确定删除？\")");
                }
            }
        }

        protected void dgList_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            DataAccess.Operate.DA_Report_Attach_Operate Report_Attach_Operate = new DataAccess.Operate.DA_Report_Attach_Operate();
            if (e.CommandName.ToString() == "Delete")
            {
                try
                {
                    Report_Attach_Operate.Delete(new Guid(dgList.DataKeys[e.Item.ItemIndex].ToString()));
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 5, new Guid(id), 3);
                    this.BindGrid();
                }
                catch (System.Exception ex)
                {
                    throw new Exception(ex.Message.ToString());
                }
                finally
                {
                    Report_Attach_Operate = null;
                }
            }
        }
    }
}
